1
|
|
|
const { Guard } = require('./guard'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* An edge object represents a edge between two nodes in a graph. In graph theory, edges are also called lines or links. |
5
|
|
|
*/ |
6
|
|
|
class JgfEdge { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Constructor |
10
|
|
|
* @param {string} source Source node ID. |
11
|
|
|
* @param {string} target Target node ID. |
12
|
|
|
* @param {string|null} relation Edge relation (aka 'relationship type'). |
13
|
|
|
* @param {string|null} label Edge label (the display name of the edge). |
14
|
|
|
* @param {object|null} metadata Custom edge meta data. |
15
|
|
|
* @param {boolean|null} directed Pass true for a directed edge, false for undirected. |
16
|
|
|
*/ |
17
|
|
|
constructor(source, target, relation = null, label = null, metadata = null, directed = true) { |
18
|
|
|
this.source = source; |
19
|
|
|
this.target = target; |
20
|
|
|
this.relation = relation; |
21
|
|
|
this.label = label; |
22
|
|
|
this.metadata = metadata; |
23
|
|
|
this.directed = directed; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
set source(source) { |
27
|
|
|
Guard.assertNonEmptyStringParameter('source', source); |
28
|
|
|
this._source = source; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
get source() { |
32
|
|
|
return this._source; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
set target(target) { |
36
|
|
|
Guard.assertNonEmptyStringParameter('target', target); |
37
|
|
|
this._target = target; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
get target() { |
41
|
|
|
return this._target; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
set metadata(metadata) { |
45
|
|
|
Guard.assertValidMetadataOrNull(metadata); |
46
|
|
|
this._metadata = metadata; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
get metadata() { |
50
|
|
|
return this._metadata; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
set directed(directed) { |
54
|
|
|
Guard.assertValidDirected(directed); |
55
|
|
|
this._directed = directed; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
get directed() { |
59
|
|
|
return this._directed; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Determines whether this edge is equal to the passed edge. |
64
|
|
|
* @param {JgfEdge} edge The edge to compare to. |
65
|
|
|
* @param {boolean} compareRelation Whether or not to compare the relation as well. |
66
|
|
|
*/ |
67
|
|
|
isEqualTo(edge, compareRelation = false) { |
68
|
|
|
return edge.source === this.source |
69
|
|
|
&& edge.target === this.target |
70
|
|
|
&& (compareRelation === false || edge.relation === this.relation); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
module.exports = { |
75
|
|
|
JgfEdge, |
76
|
|
|
}; |